package Servlets;

import Service.LoginServiceimpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/loginservlet")

public class Loginservlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public Loginservlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub

        request.setCharacterEncoding("UTF-8");

        response.setCharacterEncoding("UTF-8");

        response.setContentType("text/html;charset=UTF-8");


        String i = request.getParameter("id");

        int id = Integer.parseInt(i);
        String password = request.getParameter("password");

        LoginServiceimpl lsi = new LoginServiceimpl();

        int flag;

        try {
            flag = lsi.Check(id, password);
            if (flag == 1) {

                request.getSession().setAttribute("seid", id);

                request.getRequestDispatcher("seekerMainPage.jsp").forward(request, response);

            }
            if (flag == 2) {
                request.getSession().setAttribute("seid", id);

                request.getRequestDispatcher("employerMainPage.jsp").forward(request, response);
            } else {
                request.setAttribute("cw", "登录失败");

                request.getRequestDispatcher("login.jsp").forward(request, response);

            }

        } catch (Exception e) {
// TODO Auto-generated catch block

            e.printStackTrace();

        }

    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub

        doGet(request, response);

    }


}
